package com.example.deepclonetest.myTest;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;

import java.io.*;

@NoArgsConstructor
@AllArgsConstructor
@Data
public class Car implements Cloneable,Serializable{
    String name;
    Wheel wheel;
    transient int a;

    public Car(String name,Wheel wheel){
        this.name=name;
        this.wheel=wheel;
    }
    protected Object deepCloneDefault() throws CloneNotSupportedException {
        Car car = (Car) super.clone();
        car.wheel=(Wheel)wheel.clone();
        return car;
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }

    public Car deepCloneSerializable() throws IOException, ClassNotFoundException {
        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        ObjectOutputStream objectOutputStream=new ObjectOutputStream(byteArrayOutputStream);
        objectOutputStream.writeObject(this);

        ByteArrayInputStream bis = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return (Car)ois.readObject();
    }
}
